

namespace Pixels.Core
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Pixels.ContentTypeItems;
    using Pixels.ContentTypeItems.DataLayer;
    using Pixels.Exceptions;

    internal class ContentTypeItemAdapter
    {
        #region private variables
        private DataAccessLayer dataLayer;
        private static object synclock = new object();
        private static ContentTypeItemAdapter _contentitemadapter = null;
        #endregion

        #region Constructors
        private ContentTypeItemAdapter()
        {
            dataLayer = new DataAccessLayer();
        }
        #endregion

        #region Methods

        /// <summary>
        /// Get Singleton Instance 
        /// </summary>
        /// <returns>Instance of ContentItemAdapter</returns>
        public static ContentTypeItemAdapter GetInstance()
        {
            lock (synclock) // thread safe
            {
                if (_contentitemadapter == null)
                {
                    _contentitemadapter = new ContentTypeItemAdapter();
                }
            }
            return _contentitemadapter;
        }

        /// <summary>
        /// IContentItem Add/Edit 
        /// </summary>
        /// <param name="objContentItem">ContentItem Object</param>
        /// <returns></returns>
        public IContentTypeItem ContentItemAddEdit(IContentTypeItem objContentItem)
        {
            try
            {
                return dataLayer.ContentItemAddEdit(objContentItem);
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ContentItem could not be added/modified due to invalid data/technical issues.");
            }
        }

        /// <summary>
        /// Delete ContentItem by ContentItemID
        /// </summary>
        /// <param name="ContentItemID">ContentItemID</param>
        public void ContentItemDelete(Guid ContentItemID)
        {
            try
            {
                if (!ContentItemID.IsNull())
                    dataLayer.ContentItemDelete(ContentItemID);
                else
                    new Exception("ContentItemID is cann't be null or empty");
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ContentItem could not be deleted/Invalid ActivityID.");
            }
        }

        /// <summary>
        /// Fetch ContentItem by ContentItemID
        /// </summary>
        /// <param name="ContentItemID">ContentItemID</param>
        /// <returns></returns>
        public IContentTypeItem ContentItemFetch(Guid ContentItemID)
        {
            try
            {
                if (!ContentItemID.IsNull())
                {
                    IContentTypeItem objContentItem = dataLayer.ContentItemFetch(ContentItemID);
                    return objContentItem;
                }
                else
                {
                    new Exception("ContentItemID is cann't be null or empty");                    
                }
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");                
            }
            return null;
        }     

        /// <summary>
        /// Fetch ContentItems
        /// </summary>
        /// <returns></returns>
        public IList<IContentTypeItem> ContentItemsFetch()
        {
            try
            {
                IList<IContentTypeItem> ContentItems = dataLayer.ContentItemsFetch();
                return ContentItems;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ContentItem Detail.");
            }
        }

        /// <summary>
        /// Fetch ContentItems by ContentTypeID
        /// </summary>
        /// <param name="ContentTypeID">ContentTypeID</param>
        /// <returns></returns>
        public IList<IContentTypeItem> ContentItemsFetchByContentTypeID(Guid ContentTypeID)
        {
            try
            {
                if (!ContentTypeID.IsNull())
                {
                    IList<IContentTypeItem> ContentItems = dataLayer.ContentItemsFetchByContentTypeID(ContentTypeID);
                    return ContentItems;
                }
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ContentItem Detail.");
            }
            return null;
        }

        /// <summary>
        /// Fetch ContentItem by ExtentedListItemID
        /// </summary>
        /// <param name="ExtentedListItemID">ExtentedListItemID</param>
        /// <returns></returns>
        public IContentTypeItem ContentItemFetchByExtentedListItemID(Guid ExtentedListItemID)
        {
            try
            {
                if (!ExtentedListItemID.IsNull())
                {
                    IContentTypeItem objContentItem = dataLayer.ContentItemFetchByExtentedListItemID(ExtentedListItemID);
                    return objContentItem;
                }
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
            }

            return null;
        }

        /// <summary>
        /// Fetch ContentItem 
        /// </summary>
        /// <param name="ContentTypeID">ContentTypeID</param>
        /// <param name="ItemName">ItemName</param>
        /// <returns></returns>
        public IContentTypeItem ContentItemFetch(Guid ContentTypeID, string ItemName)
        {
            try
            {
                if (!ContentTypeID.IsNull() && !string.IsNullOrEmpty(ItemName))
                {
                    IContentTypeItem objContentItem = dataLayer.ContentTypeItemsFetch(ContentTypeID, ItemName);
                    return objContentItem;
                }
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
            }
            return null;
        }
        
        #endregion
    }
}
